// axios 二次封装
import axios from 'axios';

// pinia 持久化存储
import { useUserStore } from '../store/user';

//1. 创建axios对象
const service = axios.create();

//2. 请求拦截器
service.interceptors.request.use(config => {
    const userStore = useUserStore();
    let token = userStore.token;

    // 登录存在token后 则 如不存在不传
    if (token) {
        config.headers['Authorization'] = token;
    }

    return config;
}, error => {
    Promise.reject(error);
});

//3. 响应拦截器
service.interceptors.response.use(response => {
    //判断code码
    return response;
}, error => {
    return Promise.reject(error);
});

export default service;